<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="skill" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.introduce }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0 && data.pid !==''" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- 弹窗 -->
        <el-dialog
          title="提示"
          :visible.sync="dialogFormVisible"
          :close-on-press-escape="false"
          :close-on-click-modal="false"
        >
          <!-- 弹窗组件 -->
          <department-form
            v-if="dialogFormVisible"
            :id="DadId"
            :is-edit="isEdit"
            :is-list="isList"
            @close="dialogFormVisible=false"
            @success="hSuccess"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { delDepartmentsById, getDepartments } from '@/api/departments'
import { proDepartmentsList } from '@/utils'
import departmentForm from './departmentForm.vue'

export default {
  components: { departmentForm },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      dialogFormVisible: false,
      isEdit: false,
      DadId: '',
      isList: []
    }
  },
  created() {
    this.loadDepartmentsList()
  },
  methods: {
    async loadDepartmentsList() {
      const res = await getDepartments()
      console.log('获取组织架构数据', res)
      const arr = res.data.data.depts
      this.isList = arr
      this.list = proDepartmentsList(arr)
    },
    // 成功 关闭弹窗 重新加载
    hSuccess() {
      this.dialogFormVisible = false
      this.loadDepartmentsList()
    },
    // 添加
    hAdd(id) {
      this.dialogFormVisible = true
      this.isEdit = false
      this.DadId = id
    },
    // 修改
    async hEdit(id) {
      this.dialogFormVisible = true
      this.isEdit = true
      this.DadId = id
    },
    doEdit() {},
    hDel(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartmentsById(id)
        console.log(res)
        this.loadDepartmentsList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>
</style>
